package com.jandar;

import java.io.File;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
/*
<table>
<department>工商</department>
<name value="exchangestat">数据交换表</name>
<id value="id" type="NUMBER" generator="AUTO" sequence="hibernate_sequence.nextval">ID</id>
<column value="jhrq" type="DATETIME" format="yyyy-MM-dd HH:mm:ss">交换开始时间</column>
<column value="jhtable" type="STRING">交换表</column>
<column value="cnt" type="NUMBER">交换条数</column>
<column value="depart" type="STRING">部门</column>
<column value="fileName" type="STRING">文件名</column>
</table>
*/
public class XmlFactory {
	
	private static final String STRING = "String";
	private static final String INTEGER = "Integer";
	private static final String LONG = "Long";
	private static final String BIGDECIMAL = "BigDecimal";
	private static final String CALENDAR = "Calendar";
	private static final String BOOLEAN = "Boolean";
	
	private static final String NAME_INFO = "\t<name value=\"{value}\">{name}</name>";
	private static final String ID_INFO = "\t<id value=\"id\" type=\"NUMBER\" generator=\"AUTO\" sequence=\"hibernate_sequence.nextval\">ID</id>";
	private static final String DEPARTMENT_INFO = "\t<department>{department}</department>";
	private static final String COLUMN_INFO = "\t<column value=\"{value}\" type=\"{type}\" {unique} {format}>{column}</column>";

	public static void main(String[] args) throws Exception {
		
		String fName = "d:/xmlFactory.xls";
		Workbook book = Workbook.getWorkbook(new File(fName));
		Sheet[] sheets = book.getSheets();
		parseSheet(sheets[7]);
		book.close();
	}
	
	public static void parseSheet(Sheet sheet) {
		String department = sheet.getName();
		String departmentInfo = DEPARTMENT_INFO.replace("{department}", department);
		
		int row = sheet.getRows();
		for (int i=0; i<row; ++i) {
			Cell[] cell = sheet.getRow(i);
			if ("$".equals(cell[0].getContents())) {
				System.out.println("<table>");
				System.out.println(departmentInfo);
				String nameInfo = NAME_INFO.replace("{value}", cell[2].getContents());
				nameInfo = nameInfo.replace("{name}", cell[1].getContents());
				System.out.println(nameInfo);
				System.out.println(ID_INFO);
				
				while ((i + 1 < row) && !"$".equals(sheet.getRow(i + 1)[0].getContents())) {
					++i;
					String columnInfo = COLUMN_INFO;
					Cell[] column = sheet.getRow(i);
					String value = column[1].getContents();
					if (value.endsWith("*")) {
						columnInfo = columnInfo.replace("{value}", value.substring(0, value.length() - 1));
						columnInfo = columnInfo.replace("{unique}", "unique=\"TRUE\"");
					} else {
						columnInfo = columnInfo.replace("{value}", value);
						columnInfo = columnInfo.replace("{unique}", "");
					}
					String type = getType(column[2].getContents());
					if ("DATE".equals(type)) {
						columnInfo = columnInfo.replace("{format}", "format=\"yyyy-MM-dd\"");
					} else {
						columnInfo = columnInfo.replace("{format}", "");
					}
					columnInfo = columnInfo.replace("{type}", type);
					columnInfo = columnInfo.replace("{column}", column[0].getContents());
					
					System.out.println(columnInfo);
				}
				
				System.out.println("</table>");
			}
		}
	}
	
	public static String getType(String type) {
		String ret = null;
		if (STRING.equals(type)) {
			ret = "STRING";
		} else if (INTEGER.equals(type) || LONG.equals(type) || BIGDECIMAL.equals(type)) {
			ret = "NUMBER";
		} else if (BOOLEAN.equals(type)) {
			ret = "BOOLEAN";
		} else if (CALENDAR.equals(type)) {
			ret = "DATE";
		}
		return ret;
	}
}
